package com.vfa.vocaburay;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.vfa.data.LanguageData;
import com.vfa.data.SettingActivity;
import com.vfa.data.WordData;
import com.vfa.test.InsertQuestion;
import com.vfa.test.ListCatagory;
import com.vfa.test.ListQuestion;
import com.vfa.test.Utility;

public class vfaMain extends Activity {
	private Button toWorldList;
	private Button toQuiz;
	private Button toUnit;
	private Button toFinalTest;
	public static WordData wordData;
	public static LanguageData languageData;

	private Utility util;
	private String databasedicName ="dictionary.db";
	private String databaseQuestion ="Question.sqlite";
	private String databasePath = "data/data/com.vfa.vocaburay/databases/";
	
	
	public static boolean flag = false;

	String projectname = "/android/data/com.vfa.vocabulary/";	
	String path = Environment.getExternalStorageDirectory() + projectname;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_new);
		
		// create a File object for the parent directory
		try {
			File dataSdcard = new File(path);
			File outputFile = new File(path, "test.txt");
			// now attach the OutputStream to the file object, instead of a String representation
			FileOutputStream fos = new FileOutputStream(outputFile);
		} catch (Exception e) {
			
		}

		if (flag == false) {
			
			wordData = new WordData(getApplicationContext());
			languageData = new LanguageData(getApplicationContext());
			
			CopyData();
			flag = true;
		}
		
		toWorldList = (Button) findViewById(R.id.toWorldList);
		toQuiz = (Button) findViewById(R.id.toQuiz);
		toWorldList.setOnClickListener(toWordList_listener);
		
		toQuiz.setOnClickListener(toQuiz_listener);
		
		
		toUnit = (Button)findViewById(R.id.btn_Unit);
		toUnit.setOnClickListener(toUnit_listener);
		
		toFinalTest = (Button) findViewById(R.id.finalTest);
		toFinalTest.setOnClickListener(toFinalTest_listenner);
		
	}
	
	private android.view.View.OnClickListener toWordList_listener = new android.view.View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(vfaMain.this, WordList.class);
			startActivity(intent);
		}
	};
	
	private android.view.View.OnClickListener toFinalTest_listenner = new android.view.View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Intent i = new Intent(vfaMain.this, TestOption.class);
			startActivity(i);
			
		}
	};
	private android.view.View.OnClickListener toQuiz_listener = new android.view.View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(vfaMain.this, QuizOption.class);
			startActivity(intent);
		}
	};

	private android.view.View.OnClickListener toUnit_listener = new android.view.View.OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(vfaMain.this, LearnVocabulary.class);
			startActivity(intent);
		}
	};
		
	public void OnClickQuestionManager(View view){
		Intent intentQM = new Intent(getApplicationContext(), ListQuestion.class);
		startActivity(intentQM);
	}
		
	
	public void CopyData(){
		File file1,file2;
		file1= new File(databasePath+ databasedicName);
		file2 = new File(databasePath+ databaseQuestion);
		util = new Utility();
		if(!file1.exists())
		{
			try {
				util.CopyDatabase(getApplicationContext(), databasedicName, databasePath+databasedicName);
				System.out.println("copy data diction thanh cong");
			} catch (IOException e) {
				System.out.println("copy database dic bi loi");
				e.printStackTrace();
			}
		}else System.out.println("File question da ton tai");
		
		if(!file2.exists()){			
			try {
				util.CopyDatabase(getApplicationContext(), databaseQuestion, databasePath+databaseQuestion);
				System.out.println("copy date question thanh cong");
			} catch (IOException e) {
				System.out.println("copy database question bi loi");
				e.printStackTrace();
			}
		}
		else System.out.println("File question da ton tai");
		
		
	}
	
	private static final String SETTING = "Settings";
	   @Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// TODO Auto-generated method stub
			super.onCreateOptionsMenu(menu);
			CreateMenu(menu);
			return true;
		}

		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			super.onOptionsItemSelected(item);
			return MenuChoice(item);
		}

		private void CreateMenu(Menu menu)
	    {
	    	menu.add(0, 0, 0, SETTING);
	    }
		
	   private boolean MenuChoice(MenuItem item)
	   {
		switch (item.getItemId()) {
		case 0:
			Intent intentshare = new Intent(vfaMain.this, SettingActivity.class);
			startActivity(intentshare);
			return true;
			
		}
		return false;
		   
	   } 

}
